package com.situ.boot.controller;

import com.situ.boot.entity.AdminUserEntity;
import com.situ.boot.entity.User;
import com.situ.boot.entity.UserEntity;
import com.situ.boot.entity.Result;
import com.situ.boot.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;


    //@RequestMapping("/hello.shtml")
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        System.out.println("UserController.hello");
        return  "Hello SprintBoot";
    }

    //查询用户信息
    @RequestMapping("/selectByPage")
    @ResponseBody
    public Result selectByPage(Integer page, Integer limit, UserEntity user){
        System.out.println("page = " + page);
        System.out.println("limit = " + limit);
        System.out.println(user);
        Result result = userService.selectByPage(page, limit, user);

        return  result;
    }

    //至userList页面
    @RequestMapping("/getUserListPage")
    public String getUserListPage(Model model) {

        return "user/user_list";
    }
    //至userList页面
    @RequestMapping("/getUserAddPage")
    public String getUserAddPage(Model model) {

        return "user/user_add";
    }

    @RequestMapping("/insertUser")
    @ResponseBody
    public Result insertUser(UserEntity user){
        System.out.println(user);
        boolean isSuccess = userService.insertUser(user);
        if (isSuccess){
            return Result.success("添加成功");
        }else {
            return  Result.error("添加失败");
        }

    }

    //修改用户页面
    @RequestMapping("/updateUser")
    @ResponseBody
    public Result updateUser(UserEntity user) {
        System.out.println(user);
        boolean isSuccess = userService.updateUser(user);
        System.out.println(isSuccess);
        if (isSuccess ){
            return Result.success("修改成功");
        }else {
            return  Result.error("修改失败");
        }
    }

    //修改用户信息前获取全部信息
    @RequestMapping("/getUpdatePage")
    public String getUpdatePage(String userId, Model model ){
        System.out.println("需要修改的Userid为：" + userId);
        UserEntity user = userService.selectById(Integer.valueOf(userId));
        model.addAttribute("user",user);
        return "user/user_update";
    }

    //删除用户信息
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer uid) {
        System.out.println("待删除的用户id为" + uid);
        boolean isSuccess = userService.deleteById(uid);

        if (isSuccess) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }



    @RequestMapping("/gradeUp")
    @ResponseBody
    public Result gradeUp(String uid , String grade){
        //获取带升级对象的信息和直推人数
        System.out.println("uid = " + uid);
        System.out.println("grade = " + grade);
        Integer userId = Integer.valueOf(uid);
        Integer userGrade = Integer.valueOf(grade);


        User user = userService.selectByTeamUserId(userId);
        List<User> directList = userService.selectBySup(userId);
        Integer directNum = directList.size();
        System.out.println("直推人数为：" + directNum );

        //查询其团队人数和团队业绩
        ArrayList listAll = new ArrayList();
        List<User> list = getTeam(userId,listAll);
        System.out.println(list.toString());

        //统计团队人数和团队业绩
        Double rechargeTotal = 0.0;//团队业绩
        Integer userNum = list.size();//团队人数

        for (User user1:list) {
             rechargeTotal += Double.valueOf(user1.getRechargeTotal());
        }
        System.out.println("团队总业绩为："+ rechargeTotal);
        System.out.println("团队人数为：" + userNum);

        //待升级人
        UserEntity userEntity = new UserEntity();
        userEntity.setUid(userId);
        userEntity.setGrade(userGrade+1);
        System.out.println(userEntity);

        //进行升级
        if (userGrade == 0 ) {
            if (rechargeTotal >= 10000.00 && directNum >= 1) {
                //0升一级
                updateUser(userEntity);
                Result.success("升级一级服务商成功");
            } else {
                System.out.println("1升2失败");
                rechargeTotal = 10000.00 - rechargeTotal;
                directNum = 1 - directNum;
                System.out.println(rechargeTotal);
                System.out.println(directNum);
                if (rechargeTotal >= 0) {
                    return Result.error("升级失败，当前团队业绩差" + rechargeTotal);
                } else if (rechargeTotal < 0) {
                    return Result.error("升级失败，当前直推人数不足");
                } else {
                    return Result.error("升级失败，当前直推人数不足且团队业绩差" + rechargeTotal);
                }
            }
        }else if(userGrade == 1) {
            if (rechargeTotal >= 20000.00 && directNum >= 5) {
                //1升二级
                userService.updateUser(userEntity);
                System.out.println("升级二级服务商成功");
                Result.success("升级一级服务商成功");
            } else {
                rechargeTotal = 20000.00 - rechargeTotal;
                directNum = 5 - directNum;
                if (rechargeTotal >= 0) {
                    return Result.error("升级失败，当前团队业绩差" + rechargeTotal);
                } else if (rechargeTotal < 0) {
                    return Result.error("升级失败，当前直推人数不足");
                } else {
                    return Result.error("升级失败，当前直推人数不足且团队业绩差" + rechargeTotal);
                }
            }
        } else if (userGrade == 2){
            if (rechargeTotal >= 40000.00 && userNum >= 10){
                //升一级
                updateUser(userEntity);
                Result.success("升级一级服务商成功");
            }else{
                rechargeTotal = 40000.00 - rechargeTotal;
                userNum = 10 - userNum;
                if (rechargeTotal >=0 ){
                    return Result.error("升级失败，当前团队业绩差" + rechargeTotal);
                }else if (rechargeTotal <0 ){
                    return Result.error("升级失败，当前直推人数不足");
                }else {
                    return Result.error("升级失败，当前直推人数不足且团队业绩差" + rechargeTotal);
                }
            }
        }else if (userGrade == 3){
            if ( rechargeTotal >= 80000.00 && userNum >= 20){
                //升一级
                updateUser(userEntity);
                Result.success("升级一级服务商成功");
            }else{
                rechargeTotal = 80000.00 - rechargeTotal;
                userNum = 20 - userNum;
                if (rechargeTotal >=0 ){
                    return Result.error("升级失败，当前团队业绩差" + rechargeTotal);
                }else if (rechargeTotal <0 ){
                    return Result.error("升级失败，当前直推人数不足");
                }else {
                    return Result.error("升级失败，当前直推人数不足且团队业绩差" + rechargeTotal);
                }
            }
        }else if (userGrade ==4){
            if (rechargeTotal >= 160000.00 && userNum >= 50){
                //升一级
                updateUser(userEntity);
                Result.success("升级一级服务商成功");
            }else{
                rechargeTotal = 160000.00 - rechargeTotal;
                userNum = 50 - userNum;
                if (rechargeTotal >=0 ){
                    return Result.error("升级失败，当前团队业绩差" + rechargeTotal);
                }else if (rechargeTotal <0 ){
                    return Result.error("升级失败，当前直推人数不足");
                }else {
                    return Result.error("升级失败，当前直推人数不足且团队业绩差" + rechargeTotal);
                }
            }
        }else if (userGrade == 5){
            return  Result.error("您已是最高级服务商");
        }

        return Result.success("升级成功");
    }



    //获取团队人数和团队总业绩
    @RequestMapping("/getTeam")
    public ArrayList getTeam(Integer uid,ArrayList listAll){

        System.out.println("当前正在被查的人的uid = " + uid);

        //查看该用户是否有直推人
        List<User> list = userService.selectBySup(uid);
        //获取该用户信息
        User user = userService.selectByTeamUserId(uid);
        System.out.println(""+user.getName()+"直推人数量为:"+list.size());
        listAll.add(user);
        if (list.size()==0){
            //如果没有直推人，就把这个被查询的人放入listAll

            System.out.println(listAll.toString());
            return listAll;
        }else{
            //如果有直推人，就对这个list进行遍历
            for (User user1:list) {
             Integer userUid = user1.getUid();
             listAll = getTeam(userUid,listAll);
            }
        }
        System.out.println("————————————————————");
        return listAll;
    }

    @RequestMapping("/getRechargePage")
    public String getRechargePage (String userId,Model model){
        System.out.println("充钱的的Userid为：" + userId);
        UserEntity user = userService.selectById(Integer.valueOf(userId));
        model.addAttribute("user",user);
        return "user/user_recharge";
    }

    @RequestMapping("/Recharge")
    @ResponseBody
     public  Result Recharge (UserEntity user){
        System.out.println("当前充钱的user信息 = " + user);
        UserEntity user1 = userService.selectById(user.getUid());
        System.out.println("原User的值 = " + user1 );
        UserEntity userNew = new UserEntity();
        userNew.setUid(user.getUid());
        userNew.setMoney(user.getMoney()+user1.getMoney());
        userNew.setBuyNumber(user1.getBuyNumber()+1);
        System.out.println("修改的User = " + userNew);
        //update修改就行
        updateUser(userNew);

        //直推奖励
        UserEntity userSup = userService.selectById(user.getUid());
        userSup.setMoney(userSup.getMoney() + user.getMoney()*0.015);
        //update修改
        updateUser(userSup);


        //团队奖励
        Integer count = Rechargedigui(user.getUid(),user.getMoney());
        System.out.println(count);

        if (count == 1){
            return Result.success("充值成功了");
        }else {
            return Result.error("充值异常");
        }
    }

    @RequestMapping("/Rechargedigui")
    public Integer Rechargedigui(Integer uSuperior,Double money){
        //进入修改
        Integer count = null;
        UserEntity user = userService.selectById(uSuperior);
        //查找到他父user
        System.out.println(user);

        if(user == null){
            System.out.println("没有父User了");
            return count = 0;
        }else {
            //获取相关信息
            Integer grade = user.getGrade();
            if (grade == 1) {
                System.out.println(user.getRechargeTotal());
                user.setRechargeTotal(user.getRechargeTotal() + money * 0.01);
                System.out.println(user.getRechargeTotal());
            } else if (grade == 2) {
                System.out.println(user.getRechargeTotal());
                user.setRechargeTotal(user.getRechargeTotal() + money * 0.02);
                System.out.println(user.getRechargeTotal());
            } else if (grade == 3) {
                System.out.println(user.getRechargeTotal());
                user.setRechargeTotal(user.getRechargeTotal() + money * 0.03);
                System.out.println(user.getRechargeTotal());
            } else if (grade == 4) {
                System.out.println(user.getRechargeTotal());
                user.setRechargeTotal(user.getRechargeTotal() + money * 0.04);
                System.out.println(user.getRechargeTotal());
            } else if (grade == 5) {
                System.out.println(user.getRechargeTotal());
                user.setRechargeTotal(user.getRechargeTotal() + money * 0.05);
                System.out.println(user.getRechargeTotal());
            }
           updateUser(user);
            count = Rechargedigui(user.getSuperior(),money);
        }
        return  count =1;

    }

    @RequestMapping("/getLoginPage")
        public String getLoginPage(){
        return "user/login";
        }

    @RequestMapping("/login")
    @ResponseBody
    public Result login (String username, String password, HttpSession session) {
        System.out.println("UserController.login()");
        System.out.println("name: " + username + ", password: " + password);
        AdminUserEntity user = userService.login(username, password);
        System.out.println(user.getAid());
        // Result result = new Result(1, "登录成功");
        if (user != null) {
            System.out.println("已验证成功");
            session.setAttribute("user", user);
            return Result.success("登录成功");
        } else {
            System.out.println("已验证失败");
            return Result.error("登录失败");
        }
    }
}
